package ebusiness.ebusiness.Controllers;

import ebusiness.ebusiness.Dao.OrderDao.OrderQueryVO;
import ebusiness.ebusiness.Dao.TorlleyDao.TorlleyQueryVO;
import ebusiness.ebusiness.Dao.UserDao.UserQueryVO;
import ebusiness.ebusiness.Dao.WalletDao.WalletQueryVO;
import ebusiness.ebusiness.Entity.*;
import ebusiness.ebusiness.Entity.UserEntity.RegisteUser;
import ebusiness.ebusiness.Entity.UserEntity.User;
import ebusiness.ebusiness.Mapper.PathUtil.PagePath;
import ebusiness.ebusiness.Mapper.PathUtil.WebPath;
import ebusiness.ebusiness.Models.Front.ShoppingTorlley.CartView;
import ebusiness.ebusiness.Models.Front.ShoppingTorlley.TorlleyImp;
import ebusiness.ebusiness.Models.Front.ShoppingTorlley.Toy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Controller
public class PayController {
    private static final String waitforPay="待支付";
    private static final String waitforGood="待收货";
    /*@RequestMapping(path = WebPath.PAYSUCCESS)
    public String PaySuccess(Model model,HttpSession session,String walletid2,String password2)throws Exception{
        int walletid=Integer.valueOf(walletid2);
        int password=Integer.valueOf(password2);
        double pay=0;
        WalletQueryVO walletQueryVO=new WalletQueryVO();
        List<Integer> oroderid = (List<Integer>)session.getAttribute("orderid");
        List<Double> total=(List<Double>)session.getAttribute("totalprice");
        double result= walletQueryVO.PayOrder(walletid,password,3,total,oroderid);
        String success=null;
        if(result>0)success="success";
        else success="fail";
        model.addAttribute("pay","0");
        model.addAttribute("success",success);
        return PagePath.PaySuccess;
    }*/

    @RequestMapping(path = WebPath.Pay2)
    public String ppay(HttpSession session,Model model)throws Exception{
            String error="";
        Object oj=session.getAttribute("user");
        if(oj==null){
            error="no user";
            model.addAttribute("msg",error);
            session.setAttribute("msg",error);
            return "redirect:"+WebPath.USERORDERSEARCHLIST1;
        }
        int uid=(int)oj;
        TorlleyImp tp=(TorlleyImp)session.getAttribute("cart");

        if(tp==null){
            tp=TorlleyImp.HistoryCart(uid);
            if(tp==null){
                error="no good in cart";
                model.addAttribute("msg",error);

                session.setAttribute("msg",error);
                return "redirect:"+WebPath.USERORDERSEARCHLIST1;
            }

        }


        final int x=tp.getGoods().size();
        TorlleyQueryVO torlleyQueryVO=new TorlleyQueryVO();
        List<Torlley> list= torlleyQueryVO.getTemplories(uid);

        UserQueryVO userQueryVO=new UserQueryVO();
        List<Address> addresses =userQueryVO.getAddressByUser(uid);
        if(addresses==null||addresses.size()<=0)return "-31";
        final int aid = addresses.get(0).getAddress_id();
        final List<Integer> bs=new ArrayList<>();
        CartView cv = new CartView() {
            @Override
            public void showCart(List<Toy> goods) {
                if(x>0){
                    try {
                        Date date=new Date();
                        java.sql.Date dt=new java.sql.Date(date.getTime());
                        torlleyQueryVO.buildorder(goods,dt.toString(),"待支付",uid,3,aid);
                        bs.add(1);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        };
        tp.show(cv);

        if(bs.size()>0){

        }else {
            error="下单失败";
            model.addAttribute("msg",error);
            session.setAttribute("msg",error);
            return "redirect:"+WebPath.USERORDERSEARCHLIST1;
        }

        session.setAttribute("msg",error);
        return "redirect:"+WebPath.USERORDERSEARCHLIST1;
    }
    @RequestMapping(path = "/pp")
    public String p2(HttpSession session,Model model) throws Exception{
        String error="";
        Object oj=session.getAttribute("user");
        if(oj==null){
            error="no login";
            model.addAttribute("msg",error);
            session.setAttribute("msg",error);
            return "redirect:"+WebPath.USERORDERSEARCHLIST1;
        }
        int uid=(int)oj;
        TorlleyImp tp=(TorlleyImp)session.getAttribute("cart");

        if(tp==null){
            tp=TorlleyImp.HistoryCart(uid);
            if(tp==null){
                error="no good in cart";
                model.addAttribute("msg",error);
                session.setAttribute("msg",error);
                return "redirect:"+WebPath.USERORDERSEARCHLIST1;
            }

        }
        WalletQueryVO walletQueryVO=new WalletQueryVO();
        List<wallet> wall =walletQueryVO.getUserwalletByUId(uid);
        if(wall==null||wall.size()<=0)return "-41";
        int ps=wall.get(0).getWallet_pw();
        int wid=wall.get(0).getWallet_id();
        List<Double> totals=new ArrayList<>();
        OrderQueryVO orderQueryVO=new OrderQueryVO();
        List<Order> ords=new ArrayList<>();
        List<Integer> myorders=new ArrayList<>();


        List<Order> os= orderQueryVO.getUnpayOrder(uid,"待支付");

        if(os!=null&&os.size()>0){
            for(Order o:os){
                totals.add(o.getNut_order_form_total_price());
                myorders.add(o.getNut_order_form_id());
            }
        }else {
            error="没有待支付订单";
                model.addAttribute("msg",error);
            session.setAttribute("msg",error);
            return "redirect:"+WebPath.USERORDERSEARCHLIST1;
        }


        int rs=     walletQueryVO.PayOrder(wid,ps,3,totals,myorders);
        error="错误码:"+rs;
        model.addAttribute("msg",error);
        session.setAttribute("msg",error);
        return "redirect:"+WebPath.USERORDERSEARCHLIST1;

    }
    /*@RequestMapping(path = WebPath.ConFirm)
    public String ComfirmOrder(Model model,HttpSession session) throws Exception{
        TorlleyImp tp=null;
        if(session.getAttribute("cart")==null){
            //error handel
            model.addAttribute("error","empty cart");
            return PagePath.ConfirmOrder;
        }else {
            tp=(TorlleyImp) session.getAttribute("cart");
        }
        User user=(User) session.getAttribute("user");
        if(user!=null&&user instanceof RegisteUser){
            List<Address> addresses=null;
            WalletQueryVO walletQueryVO=new WalletQueryVO();
            List<Integer> ids=new ArrayList<>();
            for(wallet w:walletQueryVO.getUserwalletByUId(user.getId())){
                ids.add(w.getWallet_id());
            }

            final  int x=tp.getGoods().size();
            CartView cv = new CartView() {
                @Override
                public void showCart(List<Toy> goods) {
                    if(x>0){


                        model.addAttribute("walletid",ids);
                        model.addAttribute("goods",goods);

                        model.addAttribute("addresses",addresses);
                    }else {
                        model.addAttribute("error","there are no goods in cart");
                    }

                }
            };
            tp.show(cv);


        }


        return PagePath.ConfirmOrder;
    }
    @RequestMapping(path = WebPath.Pay)
    public String Pay(Model model, HttpSession session, String payway2,String  addressid2){
        TorlleyQueryVO torlleyQueryVO=new TorlleyQueryVO();

        TorlleyImp tp=null;
        int payway= Integer.valueOf(payway2);
        int addressid=Integer.valueOf(addressid2);
        if(session.getAttribute("cart")==null){
            //error handel
            model.addAttribute("error","empty cart");
            return PagePath.ConfirmOrder;
        }else {
            tp=(TorlleyImp) session.getAttribute("cart");
        }
        int user=(int) session.getAttribute("user");
        if(user>0){
            final  int x=tp.getGoods().size();
            final double total=tp.getTotal();
            final int p=payway;
            final int addid=addressid;

            CartView cv = new CartView() {
                @Override
                public void showCart(List<Toy> goods) {
                    if(x>0){
                        List<Integer> orderid=null;
                        try {
                            Date date=new Date();
                            java.sql.Date dt=new java.sql.Date(date.getTime());
                         torlleyQueryVO.buildorder(goods,dt.toString(),waitforPay,user,payway,addressid);
                        }catch (Exception e){

                        }

                       if(orderid!=null&&orderid.size()>0)  {
                           model.addAttribute("empty"," build order fail");
                       }else {
                           session.setAttribute("orderid",orderid);
                           List<Double> ps = new ArrayList<>();
                           for(Toy good:goods){
                               if(good.getSelected()>0){
                                   ps.add(good.getGood_price()*good.getNumber());
                               }
                           }
                           session.setAttribute("totals",ps);
                       }

                    }else {
                        model.addAttribute("empty"," build order fail");
                    }

                }
            };
            tp.show(cv);

        }
        return PagePath.Pay;
    }*/




}
